package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.mapper.FarmsUserMapper;
import com.dongjiantong.user.mapper.MzUserMapper;
import com.dongjiantong.user.model.FarmUser;
import com.dongjiantong.user.model.Farms;
import com.dongjiantong.user.model.MzUser;
import com.dongjiantong.user.model.dtos.FarmsUserDto;
import com.dongjiantong.user.service.FarmsService;
import com.dongjiantong.user.service.FarmsUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cao
 * @create 2017-11-21 下午7:39
 **/
@Slf4j
@Service(version = "1.0.0")
public class FarmsUserServiceImpl extends ServiceMybatis<FarmUser> implements FarmsUserService {

    @Resource
    private FarmsUserMapper farmsUserMapper;
    @Resource
    private FarmsService farmsService;

    public List<Long> getUserIdList(List<Long> farmIdList) {
        Example example = new Example(FarmUser.class);
        example.createCriteria().andIn("farm_id", farmIdList);

        List<FarmUser> farmUserList = farmsUserMapper.selectByExample(example);
        List<Long> userIdLit = new ArrayList<>();
        for (FarmUser farmUser : farmUserList) {
            userIdLit.add(farmUser.getUser_id());
        }
        userIdLit.add(-1L);
        return userIdLit;
    }

    public Farms getFarmByUserId(Long userId) {

        FarmUser farmUser = new FarmUser();
        farmUser.setUser_id(userId);
        FarmUser farmUser1 = farmsUserMapper.selectOne(farmUser);
        if (farmUser1 != null) {
            return farmsService.getFarmsById(farmUser1.getFarm_id());
        }

        return null;

    }

}
